package com.test.mywebtest.controller;

import com.alibaba.fastjson.JSON;
import com.test.mywebtest.config.R;
import com.test.mywebtest.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/testRedis")
public class RedisController {

    @Autowired
    public RedisTemplate redisTemplate;


    @RequestMapping("/redis")
    public R redis() {
        User user = new User(UUID.randomUUID().toString(), "张三", 18);
        String userStr = JSON.toJSONString(user);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user1", userStr);
        map.put("user2", userStr);
        redisTemplate.opsForValue().set("user", userStr);

        redisTemplate.opsForValue().set("user", userStr,1000, TimeUnit.SECONDS);

        redisTemplate.opsForValue().multiSet(map);

        redisTemplate.opsForHash().put("userHash", "user1", user);

        redisTemplate.opsForHash().putAll("userHash",map);

        return R.ok();
    }
}
